/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modelviewcontroller;

import folder.helper.FileChooser;
import folder.helper.Folders;
import folder.helper.MenuLayoutElement;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import layouteditor.view.MainMenuBar;
import layoutitems.CurrentLayoutElements;
import layoutitems.ImageElement;
import layoutitems.LayoutElements;
import xml.handler.LayoutParser;

/**
 *
 * @author ST
 */
public class MenuBarController {

    public static final int SAVE = 0x458c;
    public static final int SAVE_AS = 0x897c;

    public MenuBarController() throws IOException {
    }

    public static void importNewLayoutElement(JFrame jFrame) {

        FileChooser imageChooser = new FileChooser();
        File file = null;
        String name = null;
        ImageElement imageElement;

        imageChooser.setImageFileFilter();
        imageChooser.setDialogTitle("Wählen sie ein neues Layout-Element aus");
        int returnVal = imageChooser.getChooser().showOpenDialog(jFrame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = imageChooser.getChooser().getSelectedFile();
            System.out.println(file.getName());
        }
        if (file != null) {
            imageElement = new ImageElement();
            imageElement.setPicture(file.getAbsolutePath());
            name = JOptionPane.showInputDialog("Geben Sie die Bezeichnung des neuen Layout-Elements ein", "BEZEICHNUNG");
            imageElement.setName(name);
            imageElement.setBlocked(false);
            LayoutElements.standardElements.add(imageElement);
            try {
                LayoutElements.writeStandardLayoutElementsXml();
            } catch (IOException ex) {
                Logger.getLogger(MenuBarController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void openLayoutFile(JFrame jFrame) {
        FileChooser openChooser = new FileChooser();
        openChooser.setXmlFileFilter();
        openChooser.getChooser().setFileSelectionMode(JFileChooser.FILES_ONLY);
        openChooser.setDialogTitle("Bitte wählen Sie das zu öffnende Layout aus");
        openChooser.getChooser().setCurrentDirectory(new File(Folders.choosen_Save_Path));
        int returnVal = openChooser.getChooser().showOpenDialog(jFrame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String filepath = openChooser.getChooser().getCurrentDirectory().getAbsolutePath();
            filepath += "\\";
            filepath += openChooser.getChooser().getName(openChooser.getChooser().getSelectedFile());
            System.out.println(filepath);
            LayoutParser layoutParser = new LayoutParser(filepath);
            CurrentLayoutElements.allVisibleLayoutElements = layoutParser.getLayoutElementsXmlParsed();
            jFrame.repaint();
        }
    }

    public static void saveCurrentLayout(int mode, JFrame jFrame) {
        if (mode == SAVE) {
            try {
                CurrentLayoutElements.writeCurrentLayoutToXml(Folders.standard_Save_Path, Folders.getStandardSaveFileName());
            } catch (IOException ex) {
                Logger.getLogger(MainMenuBar.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (mode == SAVE_AS) {
            FileChooser saveChooser = new FileChooser();
            saveChooser.setXmlFileFilter();
            saveChooser.getChooser().setFileSelectionMode(JFileChooser.FILES_ONLY);
            saveChooser.setDialogTitle("Bitte wählen Sie den Pfad zur Ausgabe aus");
            saveChooser.getChooser().setCurrentDirectory(new File(Folders.choosen_Save_Path));
            saveChooser.getChooser().setSelectedFile(new File(Folders.getChoosenSaveFilenameSuggestion()));
            int returnVal = saveChooser.getChooser().showSaveDialog(jFrame);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                String directory = saveChooser.getChooser().getCurrentDirectory().getAbsolutePath();
                String filename = saveChooser.getChooser().getName(saveChooser.getChooser().getSelectedFile());
                Folders.choosen_Save_Path = saveChooser.getChooser().getCurrentDirectory().getAbsolutePath();
                try {
                    CurrentLayoutElements.writeCurrentLayoutToXml(directory, filename);
                } catch (IOException ex) {
                    Logger.getLogger(MainMenuBar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static void insertNewLayoutElementIntoView(ArrayList<MenuLayoutElement> submenuArray, JFrame jFrame, ActionEvent e) {
        for (MenuLayoutElement mle : submenuArray) {
            if (e.getSource() == mle.getMenuItem()) {
                if (!mle.getImageElement().getBlocked()) {
                    ImageElement insert = new ImageElement();
                    insert.clone(mle.getImageElement());
                    CurrentLayoutElements.allVisibleLayoutElements.add(insert);
                    jFrame.repaint();
                }
            }
        }
    }
}
